package org.test.cloud.stream.rocketmq.controller;

import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.test.cloud.stream.rocketmq.util.ExamplePartitionKeyExtraction;

import javax.annotation.Resource;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class OldMessageProducerController {

    public static final AtomicInteger COUNT = new AtomicInteger();

    @Resource
    private Source source;

    @GetMapping("/send")
    public String sendMessage(String message) {
        MessageBuilder<String> messageBuilder = MessageBuilder.withPayload(message);
        messageBuilder.setHeader(ExamplePartitionKeyExtraction.PARTITION_PROP_KEY, COUNT.getAndIncrement());
        source.output().send(messageBuilder.build());
        return "Message sent: " + message;
    }
}
